package com.stud.test.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.stud.common.security.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.stud.common.core.web.controller.BaseController;
import com.stud.common.core.domain.Result;
import com.stud.common.log.annotation.Log;
import com.stud.common.log.enums.BusinessType;
import com.stud.test.domain.Stud;
import com.stud.test.service.StudService;
import com.stud.common.core.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import com.stud.common.core.web.page.TableDataInfo;

/**
 * 测试Controller
 *
 * @author Stud
 * @date 2023-08-30
 */
@Api(tags = "测试")
@RestController
@RequestMapping("stud")
@RequiredArgsConstructor
public class StudController extends BaseController {
    private final StudService studService;

    @ApiOperation("查询测试列表")
    @RequiresPermissions("test:stud:list")
    @GetMapping("list")
    public TableDataInfo<Stud> list(Stud stud) {
        startPage();
        Wrapper<Stud> wrapper = Wrappers.lambdaQuery(stud);
        List<Stud> list = studService.list(wrapper);
        return getDataTable(list);
    }

    @ApiOperation("新增测试")
    @RequiresPermissions("test:stud:add")
    @Log(title = "测试", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<Boolean> add(@Validated @RequestBody Stud stud) {
        return Result.ok(studService.save(stud));
    }

    @ApiOperation("修改测试")
    @RequiresPermissions("test:stud:edit")
    @Log(title = "测试", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<Boolean> edit(@Validated @RequestBody Stud stud) {
        return Result.ok(studService.update(stud));
    }

    @ApiOperation("删除测试")
    @RequiresPermissions("test:stud:remove")
    @Log(title = "测试", businessType = BusinessType.DELETE)
    @DeleteMapping("{ids}")
    public Result<Boolean> remove(@PathVariable Long[] ids) {
        return Result.ok(studService.removeByIds(ids));
    }

    @ApiOperation("导出测试列表")
    @RequiresPermissions("test:stud:export")
    @Log(title = "测试", businessType = BusinessType.EXPORT)
    @PostMapping("export")
    public void export(HttpServletResponse response, Stud stud) {
        Wrapper<Stud> wrapper = new QueryWrapper(stud);
        List<Stud> list = studService.list(wrapper);
        ExcelUtil<Stud> util = new ExcelUtil<>(Stud. class);
        util.exportExcel(response, list, "测试数据");
    }

    @ApiOperation("获取测试详细信息")
    @RequiresPermissions("test:stud:query")
    @GetMapping(value = "{id}")
    public Result<Stud> getInfo(@PathVariable("id") Long id) {
        return Result.ok(studService.getById(id));
    }
}
